using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace WinCommand.SnippetManager
{
	/// <summary>
	/// Summary description for OrganizeCategories.
	/// </summary>
	public class OrganizeCategories : System.Windows.Forms.Form
	{
		private CategoryCollection categories;
		private ArrayList snippets;

		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button btnDown;
		private System.Windows.Forms.Button btnUp;
		private System.Windows.Forms.TextBox textName;
		private System.Windows.Forms.Label lblName;
		private System.Windows.Forms.Button btnDelete;
		private System.Windows.Forms.Button btnNew;
		private System.Windows.Forms.Label lblIntro;
		private System.Windows.Forms.ListView listViewCategories;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.Button btnOK;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public ArrayList Snippets
		{
			set { snippets = value; }
			get { return snippets; }
		}
		public CategoryCollection Categories
		{
			get { return categories; }
			set { categories = value; }
		}
		

		public OrganizeCategories()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(OrganizeCategories));
			this.listViewCategories = new System.Windows.Forms.ListView();
			this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
			this.label1 = new System.Windows.Forms.Label();
			this.btnDown = new System.Windows.Forms.Button();
			this.btnUp = new System.Windows.Forms.Button();
			this.textName = new System.Windows.Forms.TextBox();
			this.lblName = new System.Windows.Forms.Label();
			this.btnDelete = new System.Windows.Forms.Button();
			this.btnNew = new System.Windows.Forms.Button();
			this.lblIntro = new System.Windows.Forms.Label();
			this.btnCancel = new System.Windows.Forms.Button();
			this.btnOK = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// listViewCategories
			// 
			this.listViewCategories.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																								 this.columnHeader1});
			this.listViewCategories.Font = new System.Drawing.Font("Lucida Console", 8.25F);
			this.listViewCategories.FullRowSelect = true;
			this.listViewCategories.HideSelection = false;
			this.listViewCategories.Location = new System.Drawing.Point(28, 56);
			this.listViewCategories.MultiSelect = false;
			this.listViewCategories.Name = "listViewCategories";
			this.listViewCategories.Size = new System.Drawing.Size(152, 128);
			this.listViewCategories.TabIndex = 28;
			this.listViewCategories.View = System.Windows.Forms.View.Details;
			this.listViewCategories.SelectedIndexChanged += new System.EventHandler(this.listViewCategories_SelectedIndexChanged);
			// 
			// columnHeader1
			// 
			this.columnHeader1.Text = "Category";
			this.columnHeader1.Width = 148;
			// 
			// label1
			// 
			this.label1.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.label1.Location = new System.Drawing.Point(4, 32);
			this.label1.Name = "label1";
			this.label1.TabIndex = 26;
			this.label1.Text = "&Category:";
			// 
			// btnDown
			// 
			this.btnDown.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnDown.Image = ((System.Drawing.Image)(resources.GetObject("btnDown.Image")));
			this.btnDown.Location = new System.Drawing.Point(188, 80);
			this.btnDown.Name = "btnDown";
			this.btnDown.Size = new System.Drawing.Size(24, 23);
			this.btnDown.TabIndex = 23;
			this.btnDown.Click += new System.EventHandler(this.btnDown_Click);
			// 
			// btnUp
			// 
			this.btnUp.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnUp.Image = ((System.Drawing.Image)(resources.GetObject("btnUp.Image")));
			this.btnUp.Location = new System.Drawing.Point(188, 56);
			this.btnUp.Name = "btnUp";
			this.btnUp.Size = new System.Drawing.Size(24, 23);
			this.btnUp.TabIndex = 22;
			this.btnUp.Click += new System.EventHandler(this.btnUp_Click);
			// 
			// textName
			// 
			this.textName.Font = new System.Drawing.Font("Lucida Console", 8.25F);
			this.textName.Location = new System.Drawing.Point(276, 56);
			this.textName.Name = "textName";
			this.textName.Size = new System.Drawing.Size(246, 18);
			this.textName.TabIndex = 21;
			this.textName.Text = "";
			this.textName.Leave += new System.EventHandler(this.textName_Leave);
			// 
			// lblName
			// 
			this.lblName.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.lblName.Location = new System.Drawing.Point(252, 32);
			this.lblName.Name = "lblName";
			this.lblName.TabIndex = 20;
			this.lblName.Text = "Na&me:";
			// 
			// btnDelete
			// 
			this.btnDelete.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnDelete.Location = new System.Drawing.Point(108, 192);
			this.btnDelete.Name = "btnDelete";
			this.btnDelete.Size = new System.Drawing.Size(64, 23);
			this.btnDelete.TabIndex = 19;
			this.btnDelete.Text = "&Delete";
			this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
			// 
			// btnNew
			// 
			this.btnNew.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnNew.Location = new System.Drawing.Point(36, 192);
			this.btnNew.Name = "btnNew";
			this.btnNew.Size = new System.Drawing.Size(64, 23);
			this.btnNew.TabIndex = 18;
			this.btnNew.Text = "&New";
			this.btnNew.Click += new System.EventHandler(this.btnNew_Click);
			// 
			// lblIntro
			// 
			this.lblIntro.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.lblIntro.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.lblIntro.Location = new System.Drawing.Point(4, 0);
			this.lblIntro.Name = "lblIntro";
			this.lblIntro.Size = new System.Drawing.Size(344, 16);
			this.lblIntro.TabIndex = 17;
			this.lblIntro.Text = "Use this dialog box to organize your categories.";
			// 
			// btnCancel
			// 
			this.btnCancel.Anchor = System.Windows.Forms.AnchorStyles.None;
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnCancel.Location = new System.Drawing.Point(456, 192);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(72, 23);
			this.btnCancel.TabIndex = 30;
			this.btnCancel.Text = "Cancel";
			// 
			// btnOK
			// 
			this.btnOK.Anchor = System.Windows.Forms.AnchorStyles.None;
			this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnOK.Location = new System.Drawing.Point(376, 192);
			this.btnOK.Name = "btnOK";
			this.btnOK.Size = new System.Drawing.Size(72, 23);
			this.btnOK.TabIndex = 29;
			this.btnOK.Text = "OK";
			// 
			// OrganizeCategories
			// 
			this.AcceptButton = this.btnOK;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(536, 218);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnOK);
			this.Controls.Add(this.listViewCategories);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.btnDown);
			this.Controls.Add(this.btnUp);
			this.Controls.Add(this.textName);
			this.Controls.Add(this.lblName);
			this.Controls.Add(this.btnDelete);
			this.Controls.Add(this.btnNew);
			this.Controls.Add(this.lblIntro);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
			this.MinimumSize = new System.Drawing.Size(520, 240);
			this.Name = "OrganizeCategories";
			this.Text = "Organize Categories";
			this.Load += new System.EventHandler(this.OrganizeCategories_Load);
			this.ResumeLayout(false);

		}
		#endregion

		

		private void OrganizeCategories_Load(object sender, System.EventArgs e)
		{		
			SynchronizeView();
			btnUp.Enabled = btnDown.Enabled = false;
		}

		private void SynchronizeView()
		{
			this.SuspendLayout();

			listViewCategories.Items.Clear();
			foreach (Category category in categories)
			{
				ListViewItem item = new ListViewItem(category.Name);
				item.Tag = category;
				listViewCategories.Items.Add(item);
			}
			this.ResumeLayout();
		}

		private void btnNew_Click(object sender, System.EventArgs e)
		{
			// We insert a new item at the end and then select it			
			
			string name = SnippetData.GetUniqueCategoryName(categories);
			Category newCategory = new Category(name);
			categories.Add(newCategory);

//			Snippet s = new Snippet(SnippetData.GetUniqueName(snippets), "", name);
//			snippets.Add(s);

			SynchronizeView();

			SelectCategoryByName(name);
		}

		private void btnDelete_Click(object sender, System.EventArgs e)
		{
			// -1 indicates that no item is selected
			if (listViewCategories.SelectedItems.Count > 0)
			{
				if (listViewCategories.Items.Count == 1)
				{
					MessageBox.Show("You can not delete the one and only category.", "", MessageBoxButtons.OK, MessageBoxIcon.Hand);
					
				}
				else
				{
					int index = listViewCategories.SelectedIndices[0];
					string oldName = ((Category)listViewCategories.SelectedItems[0].Tag).Name;
					categories.Remove((Category)listViewCategories.SelectedItems[0].Tag);

					ArrayList newSnppetList = new ArrayList();
					foreach (Snippet s in snippets)
					{
						if (s.Category != oldName)
						{
							newSnppetList.Add(s);
						}
					}
					snippets = newSnppetList;

					SynchronizeView();

					// Now select the next item in the list, if there is one.
					if (listViewCategories.SelectedItems.Count > index)
					{
						listViewCategories.Items[index].Selected = true;					
					}

					// If we deleted the last item, we won't have anything selected. 
					// So select the new last item (unless there isn't anything left, 
					// in which case we clear the text boxes to match).
					if (listViewCategories.SelectedItems.Count == 0)
					{
						if(listViewCategories.Items.Count > 0)
						{
							listViewCategories.Items[listViewCategories.Items.Count - 1].Selected = true;
						}
						else
						{
							textName.Text = "";
						}
					}
				}
			}
		
		}

		private void btnUp_Click(object sender, System.EventArgs e)
		{
			if (listViewCategories.SelectedItems.Count > 0)
			{
				Category selectedCategory = (Category)listViewCategories.SelectedItems[0].Tag;
				string name = selectedCategory.Name;

				int selectedListViewIndex = listViewCategories.SelectedIndices[0];
				// Go on if this is not the first element in ListView
				if (selectedListViewIndex > 0)
				{
					Category nextCategory = (Category)listViewCategories.Items[selectedListViewIndex-1].Tag;

					int selectedArryIndex = Categories.IndexOf(selectedCategory);
					int nextArrayIndex = Categories.IndexOf(nextCategory);

					categories.Insert(nextArrayIndex, selectedCategory);
					categories.RemoveAt(selectedArryIndex + 1);

					SynchronizeView();

					this.SelectCategoryByName(name);

				}
			}
		
		}

		private void btnDown_Click(object sender, System.EventArgs e)
		{
			if (listViewCategories.SelectedItems.Count > 0)
			{
				Category selectedCategory = (Category)listViewCategories.SelectedItems[0].Tag;
				string name = selectedCategory.Name;

				int selectedListViewIndex = listViewCategories.SelectedIndices[0];
				// Go on if this is not the last element in ListView
				if ((selectedListViewIndex >= 0) && (selectedListViewIndex < listViewCategories.Items.Count - 1))
				{
					Category nextCategory = (Category)listViewCategories.Items[selectedListViewIndex+1].Tag;

					int selectedArryIndex = Categories.IndexOf(selectedCategory);
					int nextArrayIndex = Categories.IndexOf(nextCategory);

					categories.Insert(selectedArryIndex, nextCategory);
					categories.RemoveAt(nextArrayIndex + 1);

					SynchronizeView();

					this.SelectCategoryByName(name);

				}
			}
		
		}

		private void SelectCategoryByName(string name)
		{
			foreach(ListViewItem selectedItem in listViewCategories.Items)
			{
				if (((Category)selectedItem.Tag).Name == name)
				{
					selectedItem.Selected = true;
					break;
				}
			}
		}

		private void listViewCategories_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if (listViewCategories.SelectedItems.Count > 0)
			{
				ListViewItem item = listViewCategories.SelectedItems[0];
				Category c = (Category)item.Tag;
				textName.Text = c.Name;
				
				btnUp.Enabled = item.Index > 0;
				btnDown.Enabled = (item.Index >= 0) && (item.Index < listViewCategories.Items.Count -1);

			}
		}

		private void textName_Leave(object sender, System.EventArgs e)
		{
			if (listViewCategories.SelectedItems.Count > 0)
			{
				ListViewItem item = listViewCategories.SelectedItems[0];
				// If something has been added here, but there's nothing selected, or if
				// nothing has actually been changed, then we discard the changes...
				if (textName.Text != ((Category)item.Tag).Name )
				{
					string oldName = ((Category)item.Tag).Name;
					// OK, so they're trying to change the name of an existing snippet.
					// Check whether there's already a column with that name in existence, 
					// since we require all snippets to have unique names.
					bool nameAlreadyTaken = false;
					foreach(Category c in categories)
					{
						if (c.Name == textName.Text)
							nameAlreadyTaken = true;
					}

					if (!nameAlreadyTaken)
					{
					
						((Category)item.Tag).Name = textName.Text;

						foreach (Snippet s in snippets)
						{
							if (s.Category == oldName)
							{
								s.Category = textName.Text;
							}
						}
					
						SynchronizeView();
					
						SelectCategoryByName(textName.Text);
						
					}
					else
					{
						MessageBox.Show("A category with that name already exists. Please choose another name.", "", MessageBoxButtons.OK, MessageBoxIcon.Hand);
						textName.Text = ((Category)item.Tag).Name;
						textName.Focus();
						textName.SelectAll();
					}
				}
			}
		}
	}
}
